package com.example.moonshine.baidumap.View.Fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.moonshine.baidumap.View.Activity.BroadCastManager;
import com.example.moonshine.baidumap.View.Activity.LoginActivity;
import com.example.moonshine.baidumap.Modle.StaticUser;
import com.example.moonshine.baidumap.R;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static android.content.ContentValues.TAG;

public class SecondFragment extends Fragment {
    View view;
    String token;
    String path = "http://123.249.28.108:8082/pipe-network/logout";
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fg2, container, false);
        Button button = view.findViewById(R.id.login_out);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //发送广播
                Intent intent = new Intent();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            DialogNohttp();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                intent.putExtra("textresult","3");
                intent.setAction("fragment_home");
                BroadCastManager.getInstance().sendBroadCast(getActivity(), intent);
//                intent.putExtra("text","5");
//                intent.setAction("fragment_home_left");
//                BroadCastManager.getInstance().sendBroadCast(getActivity(), intent);
                intent.setClass(getActivity(), LoginActivity.class);
                startActivity(intent);
                getActivity().onBackPressed();
            }
        });
        return view;
    }
    public String DialogNohttp() {
        //okhttp Post请求传输Json数据
        OkHttpClient client = new OkHttpClient();
        String postStatus = null;
        Gson gson = new Gson();
        FormBody body = new FormBody.Builder()
                .build();
        Request request = new Request.Builder()
                .addHeader("accessToken", StaticUser.getToken())
                .url(path)
                .post(body)
                .build();
        try {
            Response response = client.newCall(request).execute();
            String result = response.body().string();
            Log.i(TAG,"退出"+result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return postStatus;
    }
}



/**
 * 判断是否有任务
 */
//要做的事情

//        final Handler handler = new Handler();
//         runnable = new Runnable() {
//            @Override
//            public void run() {
//                if (Indextask.indestak.equals("123"))
//                {
//                    if (StaticDete.getIndexDate().equals("456"))
//                    {
//                        postlist.clear();
//                        postAdapter.notifyDataSetChanged();
//                        StaticDete.setIndexDate("123");
//                        handler.removeCallbacks(runnable);
//                    }else {
//                        Log.i("tag", "没有任务");
//                      }
//                }else {
//                    postlist.clear();
//                    initView();
//                    Indextask.indestak="123";
//                    handler.removeCallbacks(runnable);
//                }
//            }
//        };
//        handler.postDelayed(runnable, 3000);
